package com.ksyzt.ksite2.client.console;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.ksyzt.gwt.client.common.GwtUtil;
import com.ksyzt.gwt.client.common.PostImageProxy;
import com.ksyzt.gwt.client.common.SimpleMessage;
import com.ksyzt.gwt.client.common.SysResource;
import com.ksyzt.gwt.client.common.TopMessage;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.gwt.client.mainframe.AppData;
import com.ksyzt.gwt.client.mainframe.MainFrame;
import com.ksyzt.gwt.client.mainframe.User;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.client.modulefactory.ColumnModuleFactory;
import com.ksyzt.ksite2.client.modulefactory.DraftModuleFactory;
import com.ksyzt.ksite2.client.modulefactory.NewsModuleFactory;
import com.ksyzt.ksite2.client.modulefactory.OrgnizationModuleFactory;
import com.ksyzt.ksite2.client.modulefactory.PictureModuleFactory;
import com.ksyzt.ksite2.client.modulefactory.SiteModuleFactory;
import com.ksyzt.ksite2.shared.OrgType;
import com.ksyzt.ksite2.shared.SysConst;
import com.ksyzt.ksite2.shared.modle.PUSERObj;

public class EntryConsole implements EntryPoint
{

    private AsyncCallback<PUSERObj> m_on_check_user = new AsyncCallback<PUSERObj>()
    {

	public void onSuccess(PUSERObj user)
	{

	    if (user == null)
	    {
		login();
	    } else
	    {
		showMainFrame(user);
	    }
	}

	public void onFailure(Throwable arg0)
	{
	    Window.alert(arg0.getMessage());
	    login();
	}
    };

    SimpleMessage sm;

    public void onModuleLoad()
    {

	SysResource.INSTANCE.getCss().ensureInjected();
	m_doc = new DockLayoutPanel(Unit.PX);
	RootLayoutPanel root = RootLayoutPanel.get();
	root.add(m_doc);

	sm = new SimpleMessage(SysConst.MSG_CHECK_OPERACTOR);
	m_doc.add(sm);

	ServerProxy.SYS_MANAGER.checkCurrentUser(m_on_check_user);
    }

    private void login()
    {
	GwtUtil.redirect(GwtUtil.getCurrentPageLoginURL());
    }

    protected void showMainFrame(PUSERObj user)
    {
	// GWT.log(user.toJSONString());
	if (user.getID() != 1)
	{
	    login();
	} else
	{
	    RootLayoutPanel root = RootLayoutPanel.get();
	    root.clear();
	    try
	    {
		PostImageProxy.createPostImageProxy(SysConst.FLASHID);
	    } catch (Exception e)
	    {
		SimpleMessage sm = new SimpleMessage(
			SysConst.MSG_NEED_FLASH_CONTROL);
		root.add(sm);
		return;
	    }
	    MainFrame frame = new MainFrame();

	    root.add(frame);
	    frame.setApplication(getAppData(user));

	    frame.addModuleFactory(new DraftModuleFactory(), null);

	    frame.addModuleFactory(
		    new OrgnizationModuleFactory(),
		    ColumnModuleFactory.getInitProperty()
			    .setColumnType(OrgType.OT_ORGNIZATION)
			    .setEnableDelete(true).setOnly(true));
	    frame.addModuleFactory(
		    new ColumnModuleFactory(),
		    ColumnModuleFactory.getInitProperty()
			    .setColumnType(OrgType.OT_NEWS)
			    .setEnableDelete(true).setOnly(false));

	    frame.addModuleFactory(new NewsModuleFactory(), null);

	    frame.addModuleFactory(new SiteModuleFactory(), null);

	    frame.showModule(0);
	}
    }

    public void message(String msg)
    {
	if (msg != null && msg.length() > 0)
	{
	    new TopMessage().showMesasge(msg);
	}
    }

    DockLayoutPanel m_doc;

    public AppData getAppData(PUSERObj user)
    {
	AppData app = new AppData();
	app.user = new User();
	app.user.name = user.getNAME();
	app.user.id = user.getID();
	app.user.sex = user.getSEX();
	app.user.email = user.getEMAIL();
	app.user.realname = user.getREALNAME();
	app.messageHandler = messageHandler;
	return app;
    }

    private MessageHandler messageHandler = new MessageHandler()
    {

	@Override
	public void onMessage(Object sender, Integer message, Object value)
	{

	    if (message == MessageEvent.MESSAGE)
	    {
		message((String) value);
	    }
	}
    };
}
